package com.bbxpc.bbx.utils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ProjectName: bbx-web-parent
 * @Package: com.bbxpc.bbx.utils
 * @Description: 实现两个List<Map<String, Object>>集合的合并操作
 * @Author: tdl
 * @CreateDate: 2018/11/2 上午9:31
 **/
public class MapListUtils {
    private static Logger logger = Logger.getLogger(MapListUtils.class);

    /**
     * List<Map<>>类型合并
     *
     * @param list
     * @param sublist
     * @return list
     */
    public static List<Map<String, Object>> merge(List<Map<String, Object>> list, List<Map<String, Object>> sublist, String majorKey) {
        if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(sublist)) {
            return list;
        }

        Set<String> keys = sublist.get(0).keySet();
        if (keys.size() <= 0) {
            return list;
        }

        for (Map<String, Object> item : list) {
            for (Map<String, Object> subItem : sublist) {
                if (item.get(majorKey).equals(subItem.get(majorKey))) {
                    logger.info(item.get(majorKey));
                    for (String key : keys) {
                        if (item.get(key) == null) {
                            item.put(key, subItem.get(key));
                        }
                    }
                }
            }
        }
        return list;
    }
}
